<?php 

$url = 'http://39.105.101.0/activ/api/web/index.php?r=cat/demo';
$url = 'http://39.105.101.0/activ/b.php';


//GET请求
//初始化
// $curl = curl_init();

// //设置动作
// curl_setopt($curl, CURLOPT_URL, $url);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// //执行动作
// $data = curl_exec($curl);

//POST请求

// $arr = [
// 	'name'=>'关哥闭眼了',
// 	'age'=>11,
// 	'sex'=>'男'
// ];

// $curl = curl_init();

// //设置动作
// curl_setopt($curl, CURLOPT_URL, $url);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl, CURLOPT_POST, 1);

// curl_setopt($curl, CURLOPT_POSTFIELDS, $arr);//全部类型的数据均可使用该方法发送

// $data = curl_exec($curl);

// var_dump($data);

//PUT
// $arr = [
// 	'name'=>'关哥闭眼了',
// 	'age'=>11,
// 	'sex'=>'男'
// ];

// // $arr = 'a=b';

// $a = json_encode($arr);

// $curl = curl_init();

// //设置动作
// curl_setopt($curl, CURLOPT_URL, $url);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
// // curl_setopt($curl, CURLOPT_PUT, 1);
// curl_setopt($curl, CURLOPT_CUSTOMREQUEST,"DELETE");
// curl_setopt($curl, CURLOPT_POSTFIELDS, $a);//全部类型的数据均可使用该方法发送

// $data = curl_exec($curl);

// var_dump($data);




class curl{

	private $curl;	//curl对象
	private $fields;	//要发送的参数

	public function __construct(){

		$this->curl = curl_init();
		
		curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
	}

	public function fields($arr = array()){

		$this->fields = $arr;

		return $this;
	}

	public function get($url){

		curl_setopt($this->curl, CURLOPT_URL, $url);

		//处理要发送的参数

		$fields = strstr($url, '?') ? '&' : '?';
		foreach($this->fields as $k=>$v){
			$fields .= $k.'='.$v.'&';
		}

		$fields = rtrim($fields, '&');

		curl_setopt($this->curl, CURLOPT_URL, $url.$fields);

		//执行
		$data = $this->send();
		
		//关闭资源
		$this->close();

		return $data;
	}

	public function post($url){
		
		//设置动作
		curl_setopt($this->curl, CURLOPT_URL, $url);
		curl_setopt($this->curl, CURLOPT_POST, 1);

		curl_setopt($this->curl, CURLOPT_HTTPHEADER, array());
		curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->fields);//全部类型的数据均可使用该方法发送
	
		//执行
		$data = $this->send();
		
		//关闭资源
		$this->close();

		return $data;	
	}

	public function put($url){
		
		$fields = json_encode($this->fields);

		curl_setopt($this->curl, CURLOPT_URL, $url);
		curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST,"PUT");
		curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);//全部类型的数据均可使用该方法发送

		//执行
		$data = $this->send();
		
		//关闭资源
		$this->close();

		return $data;
	}

	public function delete($url){
		
		$fields = json_encode($this->fields);

		curl_setopt($this->curl, CURLOPT_URL, $url);
		curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST,"DELETE");
		curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);//全部类型的数据均可使用该方法发送

		//执行
		$data = $this->send();
		
		//关闭资源
		$this->close();

		return $data;
	}

	private function close(){
		curl_close($this->curl);
	}

	private function send(){
		return curl_exec($this->curl);
	}
}

$arr = [
	'name'=>'关哥闭眼了',
	'age'=>11,
	'sex'=>'男'
];



$url = 'http://39.105.101.0/activ/api/web/index.php?r=cat/demo';
$url = 'http://39.105.101.0/activ/b.php';


$curl = new Curl();

$data = $curl->fields($arr)->post($url);

var_dump($data);











